﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WPF_SFrost
{
    public partial class FormDB : Form
    {
        protected string backupPath = "",restorePath = "";
        protected SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        protected SqlCommand cmd;
        public FormDB()
        {
            InitializeComponent();
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            if (txtBackup.Text == "")
            {
                MessageBox.Show("请选择有效的备份路径。", "提示");
                return;
            }
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (File.Exists(backupPath))
                {
                    File.Delete(backupPath);
                }
                string sql = string.Format("backup database addresslist to disk='{0}'", backupPath);
                conn.Open();
                cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("数据库备份成功。");
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
                Cursor = Cursors.Default;
            }
        }

        private void btnRestorePath_Click(object sender, EventArgs e)
        {
            ofdlgRestore.FilterIndex = 1;
            ofdlgRestore.FileName = "";
            ofdlgRestore.Filter = "bak Files (*.bak) | *.bak";
            if (ofdlgRestore.ShowDialog() == DialogResult.OK)
            {
                txtRestore.Text = ofdlgRestore.FileName;
            }
            restorePath = txtRestore.Text;
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            if (restorePath == "")
            {
                MessageBox.Show("请选择有效的数据库备份文件。", "提示");
                return;
            }
            try
            {
                string sql = string.Format("alter database addresslist set offline with rollback immediate;use master;restore database addresslist from disk= '{0}' with replace;alter database addresslist set online with rollback immediate", restorePath);
                conn.Open();
                cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("恢复成功。");
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
                this.Cursor = Cursors.Default;
            }
            
        }

        private void btnBackupPath_Click(object sender, EventArgs e)
        {
            sfdlgBackup.FilterIndex = 1;
            sfdlgBackup.FileName = "";
            sfdlgBackup.Filter = "Bak files (*.bak) | *.bak";
            if(sfdlgBackup.ShowDialog() == DialogResult.OK)
            {
                txtBackup.Text = sfdlgBackup.FileName;
            }
            backupPath = txtBackup.Text;
        }
    }
}
